<?php
class users extends spController{
	public function __construct() {
		//登录验证	
		if(!isset($_SESSION['IS_LOGIN']) || !$_SESSION['IS_LOGIN']){
			$this->jump (spUrl ('login','index'));
		}
		parent::__construct();
		$this -> _controller = 'users';
		$this -> _pk = spClass('MUser') -> pk;
		$this -> _attributes = spClass('MUser') -> getAttributes();
		$this -> _attributeFormset = spClass('MUser') -> getAttributeFormsets();
		$this -> savesession = $_SESSION['IS_LOGIN'];
		$this -> savesessionid = $_SESSION['LOGIN_ID'];
		$this->spAcl = spClass("spAcl")->get();
		spAddViewFunction('cbForm',array('LibCodeBuilder','cbFormWidget'));
	}
	
	public function index(){
		$this -> _attributes = spClass('MUser') -> getUnFilteAttributes();
		$this -> tpl_page = $page = $this -> spArgs('page',1);
		$this -> tpl_datas = spClass('MUser') -> spPager($page, 30) -> findAll();
		$this -> tpl_pager = spClass('MUser') -> spPager() -> getPager();
		$this -> display('users/search.html');
	}
	
	public function view(){
		$condition = array();
		$condition[$this->_pk] = $this -> spArgs('id');
		$this -> tpl_data = spClass('MUser') -> find($condition);
		$this -> display('users/view.html');
	}
	
	public function create(){
		if($_POST){
			$rows = spClass('spArgs')->get($this->_controller, null, 'post');
			$rows['pwd'] = md5($rows['pwd']);
			
			if(($id = spClass('MUser')->create($rows)))
				$this ->success ('新增数据成功',  spUrl ($this->_controller, 'view', array('id'=>$id)));
			else
				$this ->error ('新增数据失败', 'javascript:history.go(-1)');
		}
		$this ->display('users/create.html');
	}
	
	public function update(){
		$condition = array();
		$condition[$this->_pk] = $_SESSION['LOGIN_ID'];
		if($_POST){
			$oldData = spClass('MUser') -> find();
			$rows = array();

			if($oldData['pwd'] != md5($_POST['pwd']) || $_POST['newpwd'] == ''){
				$this ->error ('修改失败，旧密码不对 或 新密码不能为空！', 'javascript:history.go(-1)');
				exit;
			}
			
			$rows['pwd'] = md5($_POST['newpwd']);			
			
			if(spClass('MUser')->update($condition, $rows)){
                $this ->success ('修改成功',  spUrl ($this->_controller, 'update'));
            }else{
				$this ->error ('修改失败', 'javascript:history.go(-1)');
			}
		}
		
		$this -> tpl_data = spClass('MUser') -> find();
		
		$this ->display('users/update.html');
	}

	public function delete(){
		$condition = array();
		$condition[$this->_pk] = $this -> spArgs('id');
		$success = spClass('MUser') -> delete($condition);
		if($success)
			$this ->success('删除成功', $this->spArgs('redirect','javascript:history.go(-1)'));
		else
			$this ->error('删除失败', $this->spArgs('redirect','javascript:history.go(-1)'));
	}
	
	public function search(){
		$this -> _attributes = spClass('MUser') -> getUnFilteAttributes();
		if($_POST){
			$this -> _attributes = spClass('MUser') -> getUnFilteAttributes();
			$this -> tpl_keyword = $conditions = spClass('spArgs')->get($this->_controller, null, 'post');
			$this -> tpl_searchType = $searchType = spClass('spArgs')->get($this->_controller.'_searchType', null, 'post');
			$where = array();
			foreach($conditions as $key => $value){
				if(empty($value)) continue;
				if(empty($this->_attributes[$key])) continue;
				if($searchType[$key] == 1) $where[] = ' '.$key.' LIKE '.spClass('MUser')->escape($value).' ';
				else $where[] = ' '.$key.' LIKE '.spClass('MUser')->escape('%'.$value.'%').' ';
			}
			$this -> tpl_page = $page = $this -> spArgs('page',1);
			$this -> tpl_datas = spClass('MUser') -> spPager($page, 30) -> findAll(implode('AND',$where));
			$this -> tpl_pager = spClass('MUser') -> spPager() -> getPager();
		}else{		
			$this -> tpl_page = $page = $this -> spArgs('page',1);
			$this -> tpl_datas = spClass('MUser') -> spPager($page, 30) -> findAll();
			$this -> tpl_pager = spClass('MUser') -> spPager() -> getPager();
		}
		$this -> display('users/search.html');
	}
}